package com.guoxin.concurrentprogramming.chapter3.synccontrol;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 重入锁
 *
 * @author guoxin
 * @date 2019/7/15
 */
public class ReenterLock extends Thread {

    private static int i = 0;
    private static final Lock LOCK = new ReentrantLock();

    @Override
    public void run() {
        for (int j = 0; j < 100000; j++) {
            LOCK.lock();
            try {
                i++;
            } finally {
                LOCK.unlock();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ReenterLock reenterLock = new ReenterLock();
        Thread t1 = new Thread(reenterLock);
        Thread t2 = new Thread(reenterLock);

        t1.start();
        t2.start();


        t1.join();
        t2.join();
        System.out.println(i);
    }
}
